package com.player;

import com.formdev.flatlaf.FlatLightLaf;
import org.bytedeco.javacv.FFmpegFrameGrabber;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        FlatLightLaf.setup();


        if (args.length >= 1) {
            System.out.println(args[0]);
            Data.FILENAME = args[0];
        }
        VideoThread videoThread = new VideoThread();

        if (Objects.isNull(Data.FILENAME ) || Data.FILENAME .isEmpty()) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setFileFilter(new FileNameExtensionFilter("视频文件", "mp4", "avi", "mkv", "flv", "3gp", "mpg", "mpg4","m2ts"));
            chooser.showOpenDialog(null);
            File selectedFile = chooser.getSelectedFile();
            if (Objects.nonNull(selectedFile)) {
                Data.FILENAME = selectedFile.getAbsolutePath();
            }
        }
        if (Objects.isNull(Data.FILENAME)|| Data.FILENAME .isEmpty()) {
            return;
        }


        GetFrameFromFile getFrameFromFileThread = new GetFrameFromFile(Data.FILENAME );


        FFmpegFrameGrabber frameGrabber = getFrameFromFileThread.getFrameGrabber();
        int sampleFormat = frameGrabber.getSampleFormat();
        int sampleRate = frameGrabber.getSampleRate();
        int audioChannels = frameGrabber.getAudioChannels();
        AudioThread audioThread = new AudioThread(sampleFormat, sampleRate, audioChannels);
        Data.sourceDataLine = audioThread.getSourceDataLine();
        audioThread.start();
        videoThread.start();


        getFrameFromFileThread.run();
    }
}
